package com.zqxq.lottery.service.expert.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.common.constant.Constant;
import com.zqxq.lottery.common.utils.CommonUtil;
import com.zqxq.lottery.entity.po.export.ExpertInfoPO;
import com.zqxq.lottery.entity.po.sys.SysConfigPO;
import com.zqxq.lottery.mapper.expert.ExpertInfoMapper;
import com.zqxq.lottery.mapper.sys.SysConfigMapper;
import com.zqxq.lottery.service.expert.IExpertInfoService;
import com.zqxq.lottery.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ExpertInfoServiceImpl extends ServiceImpl<ExpertInfoMapper, ExpertInfoPO> implements IExpertInfoService {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public int expertAuth(Long user_id, String nick, String head_portrait, String introduce) {
        //查询该用户是否是专家
        Wrapper<ExpertInfoPO> wrapper = new EntityWrapper();
        wrapper.eq("user_id", user_id);
        List<ExpertInfoPO> expertInfoPOs = baseMapper.selectList(wrapper);
        ExpertInfoPO expertInfoPO = new ExpertInfoPO();
        expertInfoPO.setUser_id(user_id);
        if (CommonUtil.isNotEmpty(head_portrait)) {
            expertInfoPO.setHead_portrait(head_portrait);
        }
        if (CommonUtil.isNotEmpty(introduce)) {
            expertInfoPO.setIntroduce(introduce);
        }
        if (CommonUtil.isNotEmpty(expertInfoPOs)) {
            //昵称只能修改一次
            if (expertInfoPOs.get(0).getNick_status() == Constant.EXPERT_INFO_NICK_STATUS_1) {
            }else{
                if (CommonUtil.isNotEmpty(nick)) {
                    expertInfoPO.setNick(nick);
                }
            }
            if (CommonUtil.isNotEmpty(nick)) {
                expertInfoPO.setNick_status(Constant.EXPERT_INFO_NICK_STATUS_1);
            }
            expertInfoPO.setId(expertInfoPOs.get(0).getId());
            expertInfoPO.setUpdate_time(DateUtil.getCurrentDate());
            return baseMapper.updateById(expertInfoPO);
        } else {
            if (CommonUtil.isNotEmpty(nick)) {
                expertInfoPO.setNick(nick);
            }
            return baseMapper.insert(expertInfoPO);
        }
    }

    @Override
    public ExpertInfoPO getExpertInfo(Long user_id) {
        Wrapper<ExpertInfoPO> wrapper = new EntityWrapper();
        wrapper.eq("user_id", user_id);
        List<ExpertInfoPO> list = baseMapper.selectList(wrapper);
        return CommonUtil.isEmpty(list) ? null : list.get(0);
    }
}
